import { Logger, Controller, Get, Post, Body, UseInterceptors, UploadedFile } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express'
import * as fs from 'fs';
import { AppService } from './app.service';

const UPLOAD_DESTINATION = './runtime/storage/upload';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Post('upload')
  @UseInterceptors(FileInterceptor('file', {
    dest: UPLOAD_DESTINATION // process.env['STORAGE_UPLOAD_PATH']
  }))
  uploadFile(
    @UploadedFile() file: Express.Multer.File,
    @Body() body: any
  ) {
    Logger.log('###### Upload ######');
    console.log(new Date())
    console.log(file);
    console.log(body);

    const data = fs.readFileSync(file.path)
    console.log(data.toString())

    return ({
      errCode: '0',
      ok: true,
      result: { },
    })
  }
}
